#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'classes/TestRecord.php';

// set up the table(s)
AIR2_DBManager::init();

// clean up table from any prior aborted run
$conn = AIR2_DBManager::get_master_connection();
$conn->execute('drop table if exists test_related');
$conn->execute('drop table if exists test');
// create new table
Doctrine::createTablesFromArray(array('TestRecord', 'TestRelatedRecord'));

// create some dummy data
$count = 0;
while ( $count++ < 10 ) {
    $test_record = new TestRecord();
    $test_record->test_string = $count . '-abc';
    $test_record->save();
}

plan(35);

// init browser
$browser = new AirHttpTest();
$browser->set_user('testuser', array());

$thing = array(
    'test_string' => 'hello world'
);

/**********************
 * list HTML via GET
 */
$browser->set_content_type(AirHttpTest::$HTML);
ok( $resp = $browser->http_get('/test_crud'),
    "GET list of crud HTML");
//diag($resp);
like( $resp, '/test crud body/', "GET html type");

// everything else is json
$browser->set_content_type(AirHttpTest::$JSON);

/**********************
 * list via GET
 */
ok( $resp = $browser->http_get('/test_crud.json'),
    "GET list of crud");
//diag_dump( $resp );
ok( $thing_list = json_decode($resp), "JSON decode GET list");
foreach ($thing_list->radix as $thing_obj) {
    ok( !isset($thing_obj->test_id),
        "no PK returned in GET for " . $thing_obj->test_uuid);
}
is( $thing_list->success, true, "GET list success");
is( $thing_list->total, 10, "total 10 records in list");
is( count($thing_list->radix), $thing_list->total,
    "array count of list matches total");

/**********************
 * create via POST
 */
ok( $resp = $browser->http_post(
        '/test_crud.json',
        array( 'radix' => json_encode($thing) )
    ),
    "POST new crud");
//diag_dump( $resp );
ok( $thing_post = json_decode($resp), "JSON decode POST");
is( $thing_post->success, true, "POST success");
is( $thing_post->radix->test_string, $thing['test_string'],
    "POST returns original value");
ok( $thing_post->radix->test_uuid, "POST returns new UUID");

/**********************
 * read via GET
 */
ok( $resp = $browser->http_get(
        '/test_crud/'.$thing_post->radix->test_uuid.'.json'),
    "GET new crud");
//diag_dump( $resp );
ok( $thing_get = json_decode($resp), "JSON decode GET");
ok( !isset($thing_get->test_id), "no PK returned in GET");
is( $thing_get->success, true, "GET success");
is( $thing_get->radix->test_uuid, $thing_post->radix->test_uuid,
    "UUIDs match POST and GET");

/**********************
 * update via PUT
 */
ok( $resp = $browser->http_put(
        '/test_crud/'.$thing_post->radix->test_uuid.'.json',
        array( 'radix' => json_encode(array('test_string' => 'red')))
    ),
    "change test_string via PUT");
//diag_dump( $resp );
ok( $thing_put = json_decode($resp), "JSON decode PUT");
is( $thing_put->success, true, "PUT success");
is( $thing_put->radix->test_string, 'red', "PUT changed color");

/**********************
 * delete via DELETE
 */
ok( $resp = $browser->http_delete(
        '/test_crud/'.$thing_post->radix->test_uuid.'.json'),
    "delete crud via DELETE");
//diag_dump( $resp );
ok( $thing_delete = json_decode($resp), "JSON decode DELETE");
is( $thing_delete->success, true, "DELETE success");
is( $thing_delete->radix->test_uuid, $thing_post->radix->test_uuid,
    "UUID returned on DELETE");


// clean up table
$conn->execute('drop table test_related');
$conn->execute('drop table test');

?>
